package com.ywqf_api.controller;


import com.ywqf_api.base.BaseController;
import com.ywqf_api.dto.exc.ClientExcution;
import com.ywqf_api.dto.exc.PasswordExcution;
import com.ywqf_api.dto.exc.ToLoginExcution;
import com.ywqf_api.dto.in.UserLoginDto;
import com.ywqf_api.entity.*;
import com.ywqf_api.enums.ClientEnum;
import com.ywqf_api.enums.PasswordEnum;
import com.ywqf_api.enums.ToLoginEnum;
import com.ywqf_api.service.CustomerLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(description="login接口",produces="application/json")
@Controller
@RequestMapping("login")
public class CustomerLoginController extends BaseController {
    @Autowired
    private CustomerLoginService customerLoginService;

    /**
     * 电话号码，密码登陆
     * @param userLoginDto
     * @return
     */
    @ApiOperation(value = "电话号码，密码登陆", httpMethod = "POST", notes = "电话号码，密码登陆")
    @RequestMapping(value = "/loginPhone",method = RequestMethod.POST)
    @ResponseBody
    public Object loginPhone(UserLoginDto userLoginDto){
        String telephone=userLoginDto.getPhoneNum();
        String password=userLoginDto.getPassword();
        if (userLoginDto.getPhoneNum()==null||userLoginDto.getPhoneNum().isEmpty()){
            return new ToLoginExcution(ToLoginEnum.PHONE_NULL,null);
        }
        if (String.valueOf(userLoginDto.getType())==null || String.valueOf(userLoginDto.getType()).isEmpty()){
            return new ToLoginExcution(ToLoginEnum.STATUS_NULL,null);
        }

        UserLogin customerLogin=customerLoginService.loginPhone(telephone);
        if (customerLogin==null){
            return new ToLoginExcution(ToLoginEnum.NO_REGISTERED,null);
        }

        if (customerLogin.getStatus()!=1){
            return new ToLoginExcution(ToLoginEnum.USER_DELETE,null);
        }
        if (userLoginDto.getPassword()!=null&&customerLogin.getPassword().equals(password)){
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("telephone", telephone);
            map.put("password", password);
            return new ToLoginExcution(ToLoginEnum.SUCCESS, map);

        }

        else {
           Map<String, Object> map = new HashMap<String, Object>();
           map.put("telephone", telephone);
           map.put("password", password);
            return new ToLoginExcution(ToLoginEnum.PASSWORD_ERROR_NULL, map);
        }
    }

    /**
     * 注册用户
     * @param userLoginDto
     * @return
     */
    @ApiOperation(value = "注册用户", httpMethod = "POST", notes = "注册用户")
    @RequestMapping(value = "/addLogin",method = RequestMethod.POST)
    @ResponseBody
    public Object addLogin(UserLoginDto userLoginDto){
        if (userLoginDto.getTelephone()==null || userLoginDto.getTelephone().isEmpty()){
            return new ToLoginExcution(ToLoginEnum.PHONE_NULL,null);
        }
        if (userLoginDto.getPassword()==null || userLoginDto.getPassword().isEmpty()){
            return new ToLoginExcution(ToLoginEnum.PASSWORD_NULL,null);
        }
        String telephone=userLoginDto.getTelephone();
        String password=userLoginDto.getPassword();
        UserLogin customerLogin=customerLoginService.loginPhone(telephone);
        if (customerLogin!=null){
           return new ToLoginExcution(ToLoginEnum.PHONE_REGISTER,null);
        }

        int num=customerLoginService.addLogin(telephone,password);
        if (num<1){
            return new ToLoginExcution(ToLoginEnum.FAILED,null);
        }
        return new ToLoginExcution(ToLoginEnum.SUCCESS,null);
    }

    /**
     * 通过手机号码修改密码
     * @return
     */
    @ApiOperation(value = "通过手机号码修改密码", httpMethod = "POST", notes = "通过手机号码修改密码")
    @RequestMapping(value = "/updatePasswordByTelephone",method = RequestMethod.POST)
    @ResponseBody
    public PasswordExcution updatePasswordByTelephone(String telephone,String password){

        UserLogin userLogin = customerLoginService.loginPhone(telephone);
        if (userLogin==null){
            return new PasswordExcution(PasswordEnum.PHONE_NULL_AND_ERROR,null);
        }
        int num=customerLoginService.updateUser(telephone,password);
        if (num<1){
            return new PasswordExcution(PasswordEnum.FAILED,null);
        }
        return new PasswordExcution(PasswordEnum.SUCCESS,null);
    }

    /**
     * 通过用户id修改密码
     * @return
     */
    @ApiOperation(value = "通过用户id修改密码", httpMethod = "POST", notes = "通过用户id修改密码")
    @RequestMapping(value = "/updatePassword",method = RequestMethod.POST)
    @ResponseBody
    public Object updatePasswordByCustomerId(String telephone,String password,String newPassword){


        UserLogin userLogin=customerLoginService.loginPhone(telephone);
        if (userLogin==null){
            return new PasswordExcution(PasswordEnum.USER_NULL,null);
        }
        if (!(userLogin.getPassword()).equals(password)){
            return new PasswordExcution(PasswordEnum.PASSWORD_NOT_EQUAL,null);
        }

        int num=customerLoginService.updatePasswordByCustomerId(telephone,password,newPassword);
        if (num<1){
            return new PasswordExcution(PasswordEnum.FAILED,null);
        }
            return new PasswordExcution(PasswordEnum.SUCCESS,null);
    }

    /**
     * 修改用户电话号码
     * @return
     */
    @ApiOperation(value = "修改用户电话号码", httpMethod = "POST", notes = "修改用户电话号码")
    @RequestMapping(value = "/updateTelephone",method = RequestMethod.POST)
    @ResponseBody
    public Object updateTelephone(String newTelephone,String telephone,String password){
        int selectPastTel=customerLoginService.selectPastTel(newTelephone);

        UserLogin userLogin=customerLoginService.loginPhone(telephone);

        UserAll userAll =customerLoginService.selectAllOfTelephone(password,telephone);
        if (selectPastTel>0){
            return new PasswordExcution(PasswordEnum.PHONE_EXIST,null);
        }
        if (userLogin==null){
            return new PasswordExcution(PasswordEnum.USER_NULL,null);
        }
        if (!userLogin.getTelephone().equals(telephone)){
            return new PasswordExcution(PasswordEnum.PHONE_NOT_EQUAL,null);
        }
        if (userAll==null){
            return new PasswordExcution(PasswordEnum.ATYPISM,null);
        }






        int num=customerLoginService.updateTelephone(newTelephone,telephone,password);
        if (num<1){
            return new PasswordExcution(PasswordEnum.FAILED,null);
        }
        return new PasswordExcution(PasswordEnum.SUCCESS,null);
    }

    /**
     * 用户停车位查询
     * @param houseId
     * @return
     */
    @ApiOperation(value = "用户停车位查询", httpMethod = "POST", notes = "用户停车位查询")
    @RequestMapping(value = "/findUserPark",
            method = RequestMethod.POST)
    @ResponseBody
    public ClientExcution findUserPark(int houseId){
       ToLoginExcution findUserPark=customerLoginService.findUserPark(houseId);
       return new ClientExcution(ClientEnum.SUCCESS,findUserPark);
    }

    /**
     * 最近一次服务时间
     * @param houseId
     * @return
     */
    @ApiOperation(value = "最近一次服务时间", httpMethod = "POST", notes = "最近一次服务时间")
    @RequestMapping(value = "/findServiceEndTime",
            method = RequestMethod.POST)
    @ResponseBody
    public ClientExcution findServiceEndTime(int houseId){
        String findServiceEndTime=customerLoginService.findServiceEndTime(houseId);
        return new ClientExcution(ClientEnum.SUCCESS,findServiceEndTime);
    }

    /**
     * 物业费缴费信息
     * @param houseId
     * @return
     */
    @ApiOperation(value = "物业费缴费信息", httpMethod = "POST", notes = "物业费缴费信息")
    @RequestMapping(value = "/findAll",
            method = RequestMethod.POST)
    @ResponseBody
    public ClientExcution findAll(int houseId){
        ToLoginExcution findAll=customerLoginService.findAll(houseId);
        return new ClientExcution(ClientEnum.SUCCESS,findAll);
    }

    /**
     * 采暖费订单查询
     * @param houseId
     * @return
     */
    @ApiOperation(value = "采暖费订单查询", httpMethod = "POST", notes = "采暖费订单查询")
    @RequestMapping(value = "/heatingPayMsg",
            method = RequestMethod.POST)
    @ResponseBody
    public ClientExcution heatingPayMsg(int houseId){
        List<HeatingPaid>heatingPayMsg=customerLoginService.heatingPayMsg(houseId);

            return new ClientExcution(ClientEnum.SUCCESS, heatingPayMsg);

    }

    /**
     * 家政服务订单查询
     * @param houseId
     * @return
     */
    @ApiOperation(value = "家政服务订单查询", httpMethod = "POST", notes = "家政服务订单查询")
    @RequestMapping(value = "/housekeepingPay",
            method = RequestMethod.POST)
    @ResponseBody
    public ClientExcution housekeepingPay(int houseId){
        List<EstatePay> housekeepingPay=customerLoginService.housekeepingPay(houseId);
        return new ClientExcution(ClientEnum.SUCCESS, housekeepingPay);

    }

}
